package tk.mybatis.springboot.controller;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.springboot.api.TestClient;
import tk.mybatis.springboot.model.City;
import tk.mybatis.springboot.service.TestService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/city")
public class TestController implements TestClient {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    //注入RedisTemplate对象
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, String> template;

    @Autowired
    private TestService testService;

    @Value(value = "name")
    private String name;

    public int error() {
        return 100 / 0;
    }

    public String name() {
        logger.info("日志输出测试 Info");
        return name;
    }

    public String redis() {
        //具体调用
        Map<String, String> map = new HashMap<String, String>();
        map.put("value", "code");
        map.put("key", "keyValue");
        template.opsForHash().putAll("hashOps", map);
        return "success";
    }

    public String provider(City city) {
        return JSON.toJSONString(testService.getAll(city));
    }

    public City getById(Integer id) {
        return testService.getById(id);
    }

}
